package com.bcbim.operation_maintenance.business_department.service;

import com.bcbim.operation_maintenance_interface.business_department.dao.BusinessDepartmentDAO;
import com.bcbim.operation_maintenance.business_department.mapper.BusinessDepartmentMapper;
import com.bcbim.operation_maintenance_interface.business_department.service.BusinessDepartmentService;
import com.bcbim.operation_maintenance_interface.business_department.vo.BusinessDepartmentDetailVO;
import com.bcbim.operation_maintenance_interface.business_department.vo.BusinessDepartmentTreeVO;
import com.bcbim.operation_maintenance_interface.business_department.vo.ListBusinessDepartmentTreeVO;
import com.bcbim.operation_maintenance_interface.business_department.vo.ListBusinessDepartmentVO;
import com.bcbim.common.sys.Code;
import com.bcbim.common.sys.Constant;
import com.bcbim.operation_maintenance.department.mapper.DepartmentMapper;
import com.bcbim.operation_maintenance_interface.department.vo.DepartmentTreeVO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.bcbim.operation_maintenance_interface.space.dto.SpaceDTO;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version V 1.0.0
 * @Description：业务部service实现类
 * @Author: haomeijie
 * @Date: 2022/3/8 17:32
 **/
@Service(timeout = 60 * 1000, group = "businessDepartment", version = "1.0.0")
public class BusinessDepartmentServiceImpl implements BusinessDepartmentService {
    @Autowired
    private BusinessDepartmentMapper businessDepartmentMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<BusinessDepartmentTreeVO> listDepartmentTree(String projectId) {
        List<BusinessDepartmentTreeVO> list = businessDepartmentMapper.listDepartmentTree(projectId);
        List<BusinessDepartmentTreeVO> parentList = list.stream()
                .filter(businessDepartment -> StringUtils.isEmpty(businessDepartment.getParentId()))
                .collect(Collectors.toList());
        buildTree(list, parentList);
        return parentList;
    }

    /**
     * 组件树形结构
     */
    private void buildTree(List<BusinessDepartmentTreeVO> list, List<BusinessDepartmentTreeVO> parentList) {
        List<BusinessDepartmentTreeVO> childList = null;
        for (BusinessDepartmentTreeVO businessDepartmentTree : parentList) {
            childList = new ArrayList<>();
            for (BusinessDepartmentTreeVO businessDepartment : list) {
                if (StringUtils.isNotEmpty(businessDepartment.getParentId()) &&
                        businessDepartment.getParentId().equals(businessDepartmentTree.getId())) {
                    childList.add(businessDepartment);
                }
            }
            if (ObjectUtils.isEmpty(childList)) {
                childList = null;
            }
            businessDepartmentTree.setChild(childList);
        }
        if (ObjectUtils.isNotEmpty(childList)) {
            buildTree(list, childList);
        }
    }

    /**
     * 新增部门
     *
     * @param businessDepartmentDAO businessDepartmentDAO
     * @author xuehao
     * @createDate 2022/3/9 14:16
     * @retuen int
     */
    @Override
    public int insertBusinessDepartment(BusinessDepartmentDAO businessDepartmentDAO) {
        return businessDepartmentMapper.insertBusinessDepartment(businessDepartmentDAO);
    }

    /**
     * 判断部门名称是否同名
     *
     * @param businessDepartmentName
     * @param id
     * @author xuehao
     * @createDate 2022/3/9 14:26
     * @retuen int
     */
    @Override
    public int checkBusinessDepartmentName(String businessDepartmentName, String projectId, String id) {
        return businessDepartmentMapper.checkBusinessDepartmentName(businessDepartmentName, projectId, id);
    }

    /**
     * 用户下拉
     *
     * @author xuehao
     * @createDate 2022/3/9 14:32
     * @retuen List<Code>
     */
    @Override
    public List<Code> pullUser() {
        return businessDepartmentMapper.pullUser();
    }

    /**
     * 编辑业务部
     *
     * @param businessDepartmentDAO
     * @author xuehao
     * @createDate 2022/3/9 14:54
     * @retuen int
     */
    @Override
    public int updateBusinessDepartment(BusinessDepartmentDAO businessDepartmentDAO) {
        return businessDepartmentMapper.updateBusinessDepartment(businessDepartmentDAO);
    }

    /**
     * 查询业务部详情
     *
     * @param id
     * @author xuehao
     * @createDate 2022/3/9 15:18
     * @retuen BusinessDepartmentDetailVO
     */
    @Override
    public BusinessDepartmentDetailVO getBusinessDepartment(String id) {
        return businessDepartmentMapper.getBusinessDepartment(id);
    }

    /**
     * 查询业务部下的部门
     *
     * @param businessDepartmentId
     * @author xuehao
     * @createDate 2022/3/10 10:24
     * @retuen List<String>
     */
    @Override
    public List<String> getDepartmentIds(String businessDepartmentId) {
        return businessDepartmentMapper.getDepartmentIds(businessDepartmentId);
    }

    @Override
    public List<ListBusinessDepartmentVO> listBusinessDepartment(String name, String projectId) {
        List<ListBusinessDepartmentVO> list = businessDepartmentMapper.listBusinessDepartment(name, projectId);
        List<ListBusinessDepartmentVO> departmentList = departmentMapper.listDepartment(name, projectId);
        List<ListBusinessDepartmentVO> childList;
        for (ListBusinessDepartmentVO listBusinessDepartmentVO : list) {
            childList = new ArrayList<>();
            listBusinessDepartmentVO.setSign("1");
            for (ListBusinessDepartmentVO departmentVO : departmentList) {
                if (departmentVO.getParentId().equals(listBusinessDepartmentVO.getId())) {
                    departmentVO.setSign("2");
                    childList.add(departmentVO);
                }
            }
            if (ObjectUtils.isEmpty(childList)) {
                childList = null;
            }
            listBusinessDepartmentVO.setChildList(childList);
        }
        return list;
    }

    @Override
    public List<ListBusinessDepartmentTreeVO> listDepartment(SpaceDTO spaceDTO) {
        //查询业务部-部门列表
        List<ListBusinessDepartmentTreeVO> list = businessDepartmentMapper.listDepartment(spaceDTO);
        //根据条件查询业务部-部门列表
        List<ListBusinessDepartmentTreeVO> departmentTree = businessDepartmentMapper.listDepartmentByQuery(spaceDTO);
        if (ObjectUtils.isEmpty(spaceDTO.getFloorIds()) && StringUtils.isEmpty(spaceDTO.getSpaceTypeJson())
                && StringUtils.isEmpty(spaceDTO.getDepartmentJson()) && StringUtils.isEmpty(spaceDTO.getSpaceNumber())
                && StringUtils.isEmpty(spaceDTO.getSpaceName()) && !spaceDTO.getIsBlank()) {
            //查询全部
            List<String> id = new ArrayList<>();
            departmentTree.stream().forEach(d -> id.add(d.getId()));
            list = list.stream().filter(space -> !id.contains(space.getId())).collect(Collectors.toList());
            departmentTree.addAll(list);
        }
        //先过滤出一级
        List<ListBusinessDepartmentTreeVO> businessDepartment = departmentTree.stream()
                .filter(c -> c.getSign().equals("1")).collect(Collectors.toList());
        //根据名称进行排序
        Collections.sort(businessDepartment, new Comparator<ListBusinessDepartmentTreeVO>() {
            @Override
            public int compare(ListBusinessDepartmentTreeVO o1, ListBusinessDepartmentTreeVO o2) {
                //排序规则：按照汉字拼音首字母排序
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getBusinessDepartmentName(), o2.getBusinessDepartmentName());
            }
        });
        //未指派放首位
        for (int i = 0; i < businessDepartment.size(); i++) {
            ListBusinessDepartmentTreeVO bd = businessDepartment.get(i);
            if (bd.getBusinessDepartmentName().equals(Constant.UNASSIGNED)) {
                businessDepartment.remove(i);
                businessDepartment.add(0, bd);
                continue;
            }
        }
        List<ListBusinessDepartmentTreeVO> departmentList = departmentTree.stream()
                .filter(c -> c.getSign().equals("2")).collect(Collectors.toList());
        //根据名称进行排序
        //根据名称排序
        Collections.sort(departmentList, new Comparator<ListBusinessDepartmentTreeVO>() {
            @Override
            public int compare(ListBusinessDepartmentTreeVO o1, ListBusinessDepartmentTreeVO o2) {
                //排序规则：按照汉字拼音首字母排序
                Comparator<Object> com = Collator.getInstance(java.util.Locale.CHINA);
                return com.compare(o1.getDepartmentName(), o2.getDepartmentName());
            }
        });
        //未指派放首位
        for (int i = 0; i < departmentList.size(); i++) {
            ListBusinessDepartmentTreeVO d = departmentList.get(i);
            if (d.getDepartmentName().equals(Constant.UNASSIGNED)) {
                departmentList.remove(i);
                departmentList.add(0, d);
                continue;
            }
        }
        //整理子级
        for (ListBusinessDepartmentTreeVO business : businessDepartment) {
            List<ListBusinessDepartmentTreeVO> childList = departmentList.stream()
                    .filter(department -> department.getBusinessDepartmentId().equals(business.getId())).collect(Collectors.toList());
            if (ObjectUtils.isEmpty(childList)) {
                childList = null;
            }
            business.setChildList(childList);
        }
        return businessDepartment;
    }

    @Override
    public String getUnassignedId(String unassignedName, String projectId) {
        return businessDepartmentMapper.getUnassignedId(unassignedName, projectId);
    }

    @Override
    public List<DepartmentTreeVO> listDepartmentExcelTree(String projectId) {
        List<DepartmentTreeVO> list = businessDepartmentMapper.listDepartmentExcelTree(projectId);
        List<DepartmentTreeVO> parentList = list.stream()
                .filter(businessDepartment -> StringUtils.isEmpty(businessDepartment.getParentId()))
                .collect(Collectors.toList());
        buildExcelTree(list, parentList);
        return parentList;
    }

    /**
     * 组件树形结构
     */
    private void buildExcelTree(List<DepartmentTreeVO> list, List<DepartmentTreeVO> parentList) {
        List<DepartmentTreeVO> childList = null;
        for (DepartmentTreeVO businessDepartmentTree : parentList) {
            childList = new ArrayList<>();
            for (DepartmentTreeVO businessDepartment : list) {
                if (StringUtils.isNotEmpty(businessDepartment.getParentId()) &&
                        businessDepartment.getParentId().equals(businessDepartmentTree.getId())) {
                    childList.add(businessDepartment);
                }
            }
            if (ObjectUtils.isEmpty(childList)) {
                childList = null;
            }
            businessDepartmentTree.setChild(childList);
        }
        if (ObjectUtils.isNotEmpty(childList)) {
            buildExcelTree(list, childList);
        }
    }
}
